home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_349 / med / source / med200src.zoo / med.c < prev    next >
C/C++ Source or Header  |  1990-04-08  |  9KB  |  304 lines

  1. /* MED - music editor by Teijo Kinnunen 1989, 1990 */
  2. #ifndef PRECOMPILED    /* These files are precompiled to med.sym */
  3. #include <exec/types.h>
  4. #include <exec/io.h>
  5. #include <exec/errors.h>
  6. #include <exec/memory.h>
  7. #include <exec/tasks.h>
  8. #include <devices/audio.h>
  9. #include <devices/timer.h>
  10. #include <libraries/dos.h>
  11. #include <libraries/dosextens.h>
  12. #include <intuition/screens.h>
  13. #include <intuition/intuition.h>
  14. #include <hardware/cia.h>
  15. #include <proto/exec.h>
  16. #include <proto/graphics.h>
  17. #include <proto/intuition.h>
  18. #include <proto/dos.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #define PRECOMPILED
  22. #else /* For precompiling */
  23. #include <exec/interrupts.h>
  24. #include <hardware/intbits.h>
  25. #include <hardware/custom.h>
  26. #include <math.h>
  27. #include "med.h"
  28. void VapautaSoittimet(),Poistu(),SammutaAanet(),Alku(),FreeAllBlocks();
  29. extern void __asm PaivitaNaytto(register __d0 BOOL);
  30. extern void __asm AddMIDIData(register __a0 char *,register __d0 ULONG);
  31. extern void NollaaVarit(),AudioRem(),LataaSoitinLista(),VapautaSoitinLista();
  32. extern void DispInfoGadgets(),PylvKesk();
  33. extern BOOL __asm KasitteleViesti(register __d0 ULONG,register __d1 UWORD,
  34.                   register __d2 UWORD,register __a0 APTR);
  35. extern UWORD periodit[],chip medpic0[],chip medpic1[],chip mouse1[];
  36. extern struct CIA far ciaa;
  37. extern struct Image far kuviot;
  38. extern struct TextAttr far nsfont;
  39. extern struct NewWindow far nw,far pnw;
  40. extern struct NewScreen far ns;
  41. extern struct Gadget far gadget1[],far gadget3[];
  42. extern struct Custom far custom;
  43. extern UWORD chip pylvaatdata[];
  44. struct Image pylvasimage = { 0,0,320,56,2,pylvaatdata,0x3,0x0,NULL };
  45. struct RastPort *wrp;
  46. struct ViewPort *vp;
  47. struct BitMap piirtobm,nayttobm;
  48. struct TextFont *topaz8;
  49. struct Task *maintsk;
  50. struct Kappale far tamakappale;
  51. struct SoundTrackerKappale far ladattu;
  52. struct Soitin *soitin[32];
  53. extern long AudioInit();
  54. extern struct Lohko far *lohko[50];
  55. extern UBYTE firstdisptrk;
  56. extern BOOL AlustaKappale();
  57. extern BPTR currdir;
  58. UWORD *sprptr;
  59. BOOL aanipaalla[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },naytonpaivitys = TRUE;
  60. ULONG nayttomsk,hyppymsk,recmidimsk;
  61. LONG pylvasalku0 = 0L,pylvasalku1 = 0L,pylvasalku2 = 0L,plane0ptr = 0L;
  62. UBYTE pylvkork[31] = { 0 },far recmidi; /* joka pylvään korkeus */
  63. static struct Interrupt far pylvintr = { { NULL,NULL,NT_INTERRUPT,0,
  64.                 "MEDVBInt!" },(APTR)pylvkork,PylvKesk };
  65. static char *initerror[] = { NULL,"MED: Can't allocate signal bit",
  66.     "MED: Can't open audio.device (channels in use??)",
  67.     "MED: Can't open ciaa.resource","MED: ciaa timer A in use" };
  68. struct Screen *screen;
  69. struct Window *window,*pw;
  70. extern char *verstr;
  71. static long naytonpvsig = -1,hyppysig = -1,recmidisig = -1;
  72.  
  73. void _main(cmdline)
  74. char *cmdline;
  75. {
  76.     struct IntuiMessage *imsg;
  77.     register ULONG luokka;
  78.     ULONG maski;
  79.     register UWORD koodi,maare,loppu = FALSE;
  80.     APTR entwindowptr;
  81.     register APTR iaddr;
  82.     register long odotusmaski;
  83.     struct Process *thisproc = (struct Process *)FindTask(0);
  84.     entwindowptr = thisproc->pr_WindowPtr;
  85.     Alku();
  86.     AddIntServer(INTB_VERTB,&pylvintr);
  87.     thisproc->pr_WindowPtr = (APTR)window;
  88.     odotusmaski = 1<<window->UserPort->mp_SigBit|nayttomsk|hyppymsk|recmidimsk;
  89.     for(;;) {
  90.         maski = Wait(odotusmaski);
  91.         if(maski & hyppymsk) SetPointer(window,sprptr,23,16,-1,0);
  92.         if(maski & nayttomsk) PaivitaNaytto(FALSE);
  93.         if(maski & recmidimsk) PutMNote(recmidi);
  94.         while(imsg = (struct IntuiMessage *)GetMsg(window->UserPort)) {
  95.             luokka = imsg->Class;
  96.             koodi = imsg->Code;
  97.             maare = imsg->Qualifier;
  98.             iaddr = imsg->IAddress;
  99.             ReplyMsg((struct Message *)imsg);
  100.             if(KasitteleViesti(luokka,koodi,maare,iaddr)) {
  101.                 loppu = TRUE;
  102.                 break;
  103.             }
  104.         }
  105.         if(loppu) break;
  106.     }
  107.     thisproc->pr_WindowPtr = entwindowptr;
  108.     ciaa.ciapra &= ~CIAF_LED;
  109.     RemIntServer(INTB_VERTB,&pylvintr);
  110.     Poistu(NULL);
  111. }
  112.  
  113. void Poistu(virhe)
  114. char *virhe;
  115. {
  116.     char alertteksti[100];
  117.     if(virhe) {
  118.         alertteksti[0] = 0; alertteksti[1] = 20; alertteksti[2] = 16;
  119.         strcpy(&alertteksti[3],virhe);
  120.         alertteksti[strlen(virhe)+4] = '\0';
  121.         DisplayAlert(RECOVERY_ALERT,alertteksti,30);
  122.     }
  123.     RemEqualizer();
  124.     AudioRem();
  125.     FreeSerial();
  126.     FreeAllBlocks();
  127.     if(currdir) UnLock(currdir);
  128.     FreeFDatas();
  129.     FreeBuffer();
  130.     VapautaSoittimet();
  131.     VapautaSoitinLista();
  132.     if(topaz8) CloseFont(topaz8);
  133.     if(piirtobm.Planes[0]) FreeRaster(piirtobm.Planes[0],320,120);
  134.     if(window) CloseWindow(window);
  135.     if(screen) CloseScreen(screen);
  136.     if(IntuitionBase) {
  137.         OpenWorkBench(); /* OK, =RJ=!! */
  138.         CloseLibrary(IntuitionBase);
  139.     }
  140.     if(GfxBase) CloseLibrary(GfxBase);
  141.     if(recmidisig != -1) FreeSignal(recmidisig);
  142.     if(naytonpvsig != -1) FreeSignal(naytonpvsig);
  143.     if(hyppysig != -1) FreeSignal(hyppysig);
  144.     XCEXIT(0);
  145. }
  146.  
  147. void VapautaSoittimet()
  148. {
  149.     UCOUNT slsk;
  150.     for(slsk = 1; slsk < 32; slsk++)
  151.         if(soitin[slsk]) {
  152.             FreeMem((void *)soitin[slsk],soitin[slsk]->length +
  153.                 sizeof(struct Soitin));
  154.             soitin[slsk] = NULL;
  155.             tamakappale.soitin[slsk][0] = '\0';
  156.         }
  157. }
  158.  
  159. void SammutaAanet(arg)
  160. UWORD arg;
  161. {
  162.     static UBYTE mididata[3] = { 0xb0,0x7b,0x0 }; /* MIDI: All notes off */
  163.     extern void __asm ChannelOff(register __d0 UWORD);
  164.     extern struct Gadget far gadget9[];
  165.     if(arg == 999) {
  166.         custom.dmacon = 0xf; /* kaikki pois */
  167.         if(gadget9[0].GadgetID & 0x1000)
  168.         for(mididata[0] = 0xB0; mididata[0] <= 0xBF; mididata[0]++)
  169.             AddMIDIData(mididata,3);
  170.     }
  171.     else ChannelOff(arg);
  172. }
  173.  
  174. void Alku()
  175. {
  176.     long error;
  177.     if(!(IntuitionBase = OpenLibrary("intuition.library",33)))
  178.       Poistu(NULL); /* DisplayAlert() vaatii intuition.libraryn! */
  179.     naytonpvsig = AllocSignal(-1);
  180.     hyppysig = AllocSignal(-1);
  181.     recmidisig = AllocSignal(-1);
  182.     if(naytonpvsig == -1 || hyppysig == -1 || recmidisig == -1)
  183.         Poistu(initerror[1]);
  184.     nayttomsk = 1 << naytonpvsig;
  185.     hyppymsk = 1 << hyppysig;
  186.     recmidimsk = 1 << recmidisig;
  187.     if(!(GfxBase = OpenLibrary("graphics.library",0)))
  188.         Poistu("MED: Can't open graphics.library");
  189.     if(!(screen = OpenScreen(&ns)))
  190.         Poistu("MED: Can't open screen (out of memory??)");
  191.     topaz8 = OpenFont(&nsfont);
  192.     nw.Height = screen->Height;
  193.     nw.Screen = screen;
  194.     maintsk = FindTask(0);
  195.     if(!(window = OpenWindow(&nw)))
  196.         Poistu("MED: Can't open window (out of memory??)");
  197.     ShowTitle(screen,FALSE);
  198.     wrp = window->RPort;
  199.     vp = ViewPortAddress(window);
  200.     NollaaVarit();
  201.     SetRGB4(vp,17,0xe,0xa,0xa); /* sprite */
  202.     SetRGB4(vp,18,0xf,0xf,0xf);
  203.     SetRGB4(vp,19,0x4,0x8,0xf);
  204.     InitBitMap(&nayttobm,2,320,200);
  205.     nayttobm.Planes[0] = (PLANEPTR)medpic0;
  206.     nayttobm.Planes[1] = (PLANEPTR)medpic1;
  207.     BltBitMapRastPort(&nayttobm,0,0,wrp,0,0,320,200,0xc0);
  208.     OsionValinta(&gadget1[0]);
  209.     if(screen->Height >= 256) {
  210.         DrawImage(&(screen->RastPort),&pylvasimage,0,200);
  211.         pylvasalku0 = (LONG)(screen->BitMap.Planes[0]) + 8484;
  212.         pylvasalku1 = (LONG)(screen->BitMap.Planes[1]) + 8484;
  213.         pylvasalku2 = (LONG)(screen->BitMap.Planes[2]) + 8484;
  214.     }
  215.     else {
  216.         pnw.Screen = screen;
  217.         pylvasalku1 = (LONG)(screen->BitMap.Planes[1]) + 1124;
  218.         pylvasalku2 = (LONG)(screen->BitMap.Planes[2]) + 1124;
  219.         gadget3[6].GadgetID &= ~0x1000; /* Don't highlight Eq1 */
  220.     }
  221.     InitBitMap(&piirtobm,1,320,120);
  222.     if(!(piirtobm.Planes[0] = (PLANEPTR)AllocRaster(320,120)))
  223.         Poistu("MED: No memory for bitplane");
  224.     plane0ptr = (LONG)piirtobm.Planes[0];
  225.     BltClear((char *)plane0ptr,((120 << 16) + 40),3);
  226.     wrp->Mask = 0x2; /* Draw a rectangle to bitplane 2 */
  227.     SetDrMd(wrp,COMPLEMENT);
  228.     RectFill(wrp,4,132,315,140);
  229.     wrp->Mask = 0xff;
  230.     NaytaKursori();
  231.     SetAPen(wrp,1);
  232.     SetBPen(wrp,0);
  233.     Move(wrp,276,10);
  234.     Text(wrp,verstr,5); /* version number */
  235.     SetBPen(wrp,3);
  236.     SetAPen(wrp,0);
  237.     if(error = AudioInit()) Poistu(initerror[error]);
  238.     SetPointer(window,&mouse1[0],23,16,-1,0);
  239.     LataaSoitinLista();
  240.     if(AlustaKappale(TRUE)) Poistu("MED: Not enough memory");
  241.     DispInfoGadgets();
  242.     InitEqualizer();
  243.     PaivitaNaytto(TRUE);
  244. }
  245.  
  246. void FreeBlock(num)
  247. UWORD num;
  248. {
  249.     if(lohko[num]) {
  250.         FreeMem(lohko[num],4 + lohko[num]->numtracks * ONETRKSIZE);
  251.         lohko[num] = 0L;
  252.     }
  253. }
  254.  
  255. BOOL AllocBlock(UWORD num,UBYTE tracks) /* TRUE = virhe, FALSE = ok!! */
  256. {
  257.     if(lohko[num]) {
  258.         if(lohko[num]->numtracks == tracks) return(FALSE);
  259.         FreeBlock(num);
  260.     }
  261.     if(!(lohko[num] = AllocMem(4 + ONETRKSIZE * tracks,MEMF_PUBLIC|
  262.         MEMF_CLEAR))) return(TRUE);
  263.     lohko[num]->numtracks = tracks;
  264.     return(FALSE);
  265. }
  266.  
  267. void FreeAllBlocks()
  268. {
  269.     int cnt;
  270.     for(cnt = 0; cnt < 100; cnt++) FreeBlock(cnt);
  271. }
  272.  
  273. BOOL ChangeBlockSize(num,width)
  274. UWORD num,width;
  275. {
  276.     struct Lohko *newblock,*oldblock;
  277.     UBYTE line,trk;
  278.     UWORD oldtrk,newtrk;
  279.     if(!lohko[num]) return(TRUE);
  280.     newblock = AllocMem(FOURTRKSIZE * width + 4,MEMF_PUBLIC|MEMF_CLEAR);
  281.     if(!newblock) return(TRUE);
  282.     newblock->numtracks = 4 * width;
  283.     oldblock = lohko[num];
  284.     for(line = 0; line < 64; line++) {
  285.         newtrk = newblock->numtracks * 3 * line;
  286.         oldtrk = lohko[num]->numtracks * 3 * line;
  287.         for(trk = 0; trk < min(newblock->numtracks,lohko[num]->
  288.             numtracks); trk++) {
  289.             newblock->music[newtrk + 3 * trk] =
  290.                 oldblock->music[oldtrk + 3 * trk];
  291.             newblock->music[newtrk + 3 * trk + 1] =
  292.                 oldblock->music[oldtrk + 3 * trk + 1];
  293.             newblock->music[newtrk + 3 * trk + 2] =
  294.                 oldblock->music[oldtrk + 3 * trk + 2];
  295.         }
  296.     }
  297.     lohko[num] = newblock;
  298.     FreeMem(oldblock,4 + oldblock->numtracks * ONETRKSIZE);
  299.     firstdisptrk = 0;
  300.     PaivitaNaytto(TRUE);
  301.     return(FALSE);
  302. }
  303. #endif
  304.